package com.bihu.bihudemo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bihu.bihudemo.dao.QuestionDao;
import com.bihu.bihudemo.dao.TagArticleLinkDao;
import com.bihu.bihudemo.dao.TagDao;
import com.bihu.bihudemo.entity.Question;
import com.bihu.bihudemo.entity.Tag;
import com.bihu.bihudemo.entity.vo.QuestionVo;
import com.bihu.bihudemo.service.QuestionService;
import com.bihu.bihudemo.utils.redis.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 文章(Question)表服务实现类
 *
 * @author makejava
 * @since 2020-07-15 17:10:00
 */
@Service("questionService")
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, Question> implements QuestionService {

    @Resource
    QuestionDao questionDao;

    @Resource
    TagDao tagDao;

    @Resource
    TagArticleLinkDao tagArticleLinkDao;


    @Resource
    RedisUtil redisUtil;

    @Override
    public IPage<QuestionVo> getAllQuestionWithTagsByPage(Page<QuestionVo> page) {
        IPage<QuestionVo> questionVoWithPage = questionDao.getAllArticles(page);
        List<QuestionVo> records = questionVoWithPage.getRecords();

        for (QuestionVo qv:records){
            Question question=(Question)qv;
            List<Tag> tags = tagArticleLinkDao.getTagsByArticle(question);
            qv.setTagList(tags);
        }

        page.setRecords(records);

        return page;
    }


    /**
     * 根据标签ID分类查询出文章,还要分页
     * @param page
     * @param TagID
     * @return
     */
    @Override
    public IPage<QuestionVo> getQuestionWithTagsByTagID(Page<QuestionVo> page,Integer TagID) {
        IPage<QuestionVo> questionVoWithPage = questionDao.getArticlesByTag(page, TagID);
        List<QuestionVo> records = questionVoWithPage.getRecords();
        for (QuestionVo qv:records){
            Question question=(Question)qv;

            List<Tag> tags = tagArticleLinkDao.getTagsByArticle(question);
            qv.setTagList(tags);
        }
        page.setRecords(records);
        return page;
    }

    /**
     * 根据文章标题的模糊搜索
     * @param page
     * @param title
     * @return
     */
    @Override
    public IPage<QuestionVo> getQuestionWithTagsByTitle(Page<QuestionVo> page, String title) {
        IPage<QuestionVo> questionVoWithPage = questionDao.getArticlesByName(page, title);
        List<QuestionVo> records = questionVoWithPage.getRecords();
        for (QuestionVo qv:records){
            Question question=(Question)qv;

            List<Tag> tags = tagArticleLinkDao.getTagsByArticle(question);
            qv.setTagList(tags);
        }
        page.setRecords(records);
        return page;
    }

    /**
     * 缓存中的阅读数同步到数据库
     */
    @Override
    public void SynViewCount2DB() {
        //获取缓存中所有与阅读相关的信息
        Set<String> keys = redisUtil.keys("question:*");
        //根据缓存key拿到文章ID和阅读数
        List<String> idList = keys.stream().map(s -> {
            return s.substring("question:".length());
        }).collect(Collectors.toList());
        for (String id:idList){
            Object count = redisUtil.hGet(id, "viewCount");
            questionDao.getOneByID(Integer.parseInt(id));
        }

        //同步到数据库
    }
}